package kortmann.rene.leo.service;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import kortmann.rene.leo.exceptions.ForbiddenException;
import kortmann.rene.leo.exceptions.LeoNotFoundException;
import kortmann.rene.leo.exceptions.LoginFailedException;
import kortmann.rene.leo.factories.LeoFactory;
import kortmann.rene.leo.model.LEO;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

public class Api 
{	
	private static final String LOGIN_URL = "http://localhost:91/leo/login.php";
	
	private CookieStore store = null;
	private HttpContext localContext = null;
	private HttpClient client = null;
	
	private LeoApi leoApi = null;
	
	public Api(String inUser, String inPassword)
		throws ClientProtocolException, IOException, LoginFailedException
	{		
		init();
		login(inUser, inPassword);
	}

	private void init() 
	{
		leoApi = new LeoApi();
		
		store = new BasicCookieStore();
		localContext = new BasicHttpContext();
		localContext.setAttribute(ClientContext.COOKIE_STORE, store);
		client = new DefaultHttpClient();
	}
	
	private void login(String inUser, String inPassword) throws ClientProtocolException, IOException, LoginFailedException
	{
		HttpPost httpPost = null;
		
		try
		{
			List<BasicNameValuePair> parameters = new LinkedList<BasicNameValuePair>();
			parameters.add(new BasicNameValuePair("username", inUser));
			parameters.add(new BasicNameValuePair("password", inPassword));
			
			HttpEntity e = new UrlEncodedFormEntity(parameters);
			
			httpPost = new HttpPost(LOGIN_URL);
			httpPost.setEntity(e);
			
			HttpResponse response = client.execute(httpPost, localContext);
			 
			if(response.getStatusLine().getStatusCode() != 200)
			{
				throw new LoginFailedException(response.getStatusLine().getStatusCode());
			}	
			
			response = null;
		}
		finally
		{
			if(httpPost != null)
			{
				httpPost.releaseConnection();
				httpPost = null;
			}
		}
	}
	
	public List<LEO> getAllLEOs() throws LeoNotFoundException, ForbiddenException
	{
		return this.leoApi.getAllLEOs(client, localContext);
	}
	
	public LEO getLEO(int inID) throws LeoNotFoundException, ForbiddenException
	{
		return this.leoApi.getLEO(inID, client, localContext);
	}
	
	public LEO createLEO(LEO inLeo) throws LeoNotFoundException, ForbiddenException
	{
		return this.leoApi.createLEO(LeoFactory.getJsonFromLeo(inLeo), client, localContext);
	}
	
	public LEO updateLEO(LEO inLEO) throws LeoNotFoundException, ForbiddenException
	{
		return this.leoApi.updateLEO(inLEO, client, localContext);
	}
	
	public boolean deleteLeo(String inID) throws LeoNotFoundException, ForbiddenException
	{
		return this.leoApi.deleteLEO(inID, client, localContext);
	}
}
